/*
 * MIT License
 *
 * Copyright (c) 2025 Mark·虎
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.hsxxherp.key.support.sys.controller;

import com.hsxxherp.key.support.sys.entity.SysModule;
import com.hsxxherp.key.support.sys.service.ISysModuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内部api接口： 模块管理
 *
 * @author Mark·虎克（2486970363@qq.com）
 */
@RestController
@RequestMapping("/sys/modules")
@Tag(name = "应用模块管理", description = "应用模块相关操作接口")
public class ApplicationModuleController {
    @Autowired
    private ISysModuleService moduleService;

    /**
     * 获取指定模块的所有模块
     *
     * @param parentId 模块主键
     * @return 模块列表
     */
    @GetMapping("/sub-modules/{parentId}")
    @Operation(summary = "获取指定父模块下的所有子模块", description = "根据父模块 ID 获取其所有子模块信息")
    public List<SysModule> getSubModules(@PathVariable Long parentId) {
        return moduleService.getSubModules(parentId);
    }

    /**
     * 创建模块
     *
     * @param module 模块信息
     * @return 新增后模块信息
     */
    @PostMapping
    @Operation(summary = "创建新的应用模块", description = "创建一个新的应用模块，并自动生成内码")
    public SysModule createModule(@RequestBody SysModule module) {
        moduleService.save(module);
        return module;
    }

    /**
     * 重算所有模块内码
     *
     * @return 成功
     */
    @PostMapping("/recalculate-innercodes")
    @Operation(summary = "重算所有模块的内码", description = "重新计算所有应用模块的内码并更新到数据库")
    public String recalculateInnerCodes() {
        moduleService.recalculateAllInnerCodes();
        return "内码重算成功";
    }
}